package com.omybug.study.entity;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author: omybug
 * @date: 18/9/4 15:18
 */
public class JsonResponse {

    private Integer ret;

    private Object data;

    private String st;

    private String msg;

    private JsonResponse(){
        this.st = String.valueOf(System.currentTimeMillis() / 1000);
        this.msg = "";
        this.ret = 0;
        this.data = "";
    }

    public static JsonResponse success(){
        JsonResponse jr = new JsonResponse();
        jr.setRet(1);
        return jr;
    }

    public static JsonResponse success(Object data){
        JsonResponse jr = new JsonResponse();
        jr.setRet(1);
        jr.setData(data);
        return jr;
    }

    public static JsonResponse fail(String msg){
        JsonResponse jr = new JsonResponse();
        jr.setRet(-1);
        jr.setMsg(msg);
        return jr;
    }

    public static JsonResponse fail(Integer ret, String msg){
        JsonResponse jr = new JsonResponse();
        jr.setRet(ret);
        jr.setMsg(msg);
        return jr;
    }

    public Integer getRet() {
        return ret;
    }

    public void setRet(Integer ret) {
        this.ret = ret;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getSt() {
        return st;
    }

    public void setSt(String st) {
        this.st = st;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        ObjectMapper om = new ObjectMapper();
        try {
            return om.writeValueAsString(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.toString();
    }
}
